#region Using

using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;

using Gizmox.WebGUI.Common;
using Gizmox.WebGUI.Forms;
using WaveMaster.Holidays.Club.Domain;

#endregion

namespace WaveMaster.Holidays.Club.Web
{
    public partial class MainForm : Form
    {
        UserControlLogin m_loginForm;
        User m_user;
        UserControlFactory m_userControlFactory;

        public MainForm()
        {
            InitializeComponent();
            ShowMainFormPanel(false);
            initLoginForm();
            initTreeNodeAferSelect();
            
        }

        private void initTreeNodeAferSelect()
        {
            treeNodeUser.AfterSelect += new TreeViewEventHandler(treeNodeUser_AfterSelect);
            treeNodeUserRole.AfterSelect += new TreeViewEventHandler(treeNodeUserRole_AfterSelect);
            treeNodeConfiguration.AfterSelect+=new TreeViewEventHandler(treeNodeConfiguration_AfterSelect);
            treeNodeTravelAgent.AfterSelect += new TreeViewEventHandler(treeNodeTravelAgent_AfterSelect);
            treeNodeTicketPurchase.AfterSelect += new TreeViewEventHandler(treeNodeTicketPurchase_AfterSelect);
            treeNodePassport.AfterSelect += new TreeViewEventHandler(treeNodePassport_AfterSelect);
            treeNodeSeaPort.AfterSelect += new TreeViewEventHandler(treeNodeSeaPort_AfterSelect);
            treeNodeVessel.AfterSelect += new TreeViewEventHandler(treeNodeVessel_AfterSelect);
            treeNodeTrip.AfterSelect += new TreeViewEventHandler(treeNodeTrip_AfterSelect);
            treeNodeBooking.AfterSelect += new TreeViewEventHandler(treeNodeBooking_AfterSelect);
            treeNodeCheckIn.AfterSelect += new TreeViewEventHandler(treeNodeCheckIn_AfterSelect);
            treeNodeConfirmBooking.AfterSelect += new TreeViewEventHandler(treeNodeConfirmBooking_AfterSelect);
            treeNodeConfirmBooking_b.AfterSelect += new TreeViewEventHandler(treeNodeConfirmBookingB_AfterSelect);
            treeNodeTicketPurchaseReport.AfterSelect += new TreeViewEventHandler(treeNodeTicketPurchaseReport_AfterSelect);
            treeNodeBookingReport.AfterSelect += new TreeViewEventHandler(treeNodeBookingReport_AfterSelect);
            treeNodeTravelAgentReport.AfterSelect += new TreeViewEventHandler(treeNodeTravelAgentReport_AfterSelect);
            treeNodeTravelAgentSummary.AfterSelect += new TreeViewEventHandler(treeNodeTravelAgentSummary_AfterSelect);
            treeNodeBoarding.AfterSelect += new TreeViewEventHandler(treeNodeBoarding_AfterSelect);
            treeNodeMemberType.AfterSelect += new TreeViewEventHandler(treeNodeMemberType_AfterSelect);
            treeNodeTicketDeduction.AfterSelect += new TreeViewEventHandler(treeNodeTicketDeduction_AfterSelect);
            treeNodeTicketDeductionReport.AfterSelect += new TreeViewEventHandler(treeNodeTicketDeductionReport_AfterSelect);
            treeNodeTransferPassenger.AfterSelect += new TreeViewEventHandler(treeNodeTransferPassenger_AfterSelect);
            treeNodeTicketReport.AfterSelect += new TreeViewEventHandler(treeNodeTicketReport_AfterSelect);
            treeNodePassengerReport.AfterSelect += new TreeViewEventHandler(treeNodePassengerReport_AfterSelect);
        }

        void treeNodePassengerReport_AfterSelect(object sender, TreeViewEventArgs e)
        {
            try
            {
                loadUserControl(UserControlFactory.PASSENGER_REPORT);
            }
            catch (Exception x)
            {
                MessageBox.Show(x.Message, "Error Exception", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
            }
            
        }

        void treeNodeTicketReport_AfterSelect(object sender, TreeViewEventArgs e)
        {
            try
            {
                loadUserControl(UserControlFactory.TICKET_REPORT);
            }
            catch (Exception x)
            {
                MessageBox.Show(x.Message, "Error Exception", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
            }
        }

        void treeNodeTransferPassenger_AfterSelect(object sender, TreeViewEventArgs e)
        {
            try
            {
                loadUserControl(UserControlFactory.TRANSFER_PASSENGER);
            }
            catch (Exception x)
            {
                MessageBox.Show(x.Message, "Error Exception", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
            }
        }

        void treeNodeTicketDeductionReport_AfterSelect(object sender, TreeViewEventArgs e)
        {
            try
            {
                loadUserControl(UserControlFactory.TICKET_DEDUCTION_REPORT);
            }
            catch (Exception x)
            {
                MessageBox.Show(x.Message, "Error Exception", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
            }
        }

        void treeNodeTicketDeduction_AfterSelect(object sender, TreeViewEventArgs e)
        {
            try
            {
                loadUserControl(UserControlFactory.TICKET_DEDUCTION);
            }
            catch (Exception x)
            {
                MessageBox.Show(x.Message, "Error Exception", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
            }
        }

        void treeNodeMemberType_AfterSelect(object sender, TreeViewEventArgs e)
        {
            try
            {
                loadUserControl(UserControlFactory.MEMBER_TYPE);
            }
            catch (Exception x)
            {
                MessageBox.Show(x.Message, "Error Exception", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
            }
        }

        void treeNodeConfirmBookingB_AfterSelect(object sender, TreeViewEventArgs e)
        {
            try
            {
                loadUserControl(UserControlFactory.CONFIRM_BOOKING);
            }
            catch (Exception x)
            {
                MessageBox.Show(x.Message, "Error Exception", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
            }
        }

        void treeNodeBooking_AfterSelect(object sender, TreeViewEventArgs e)
        {
            try
            {
                loadUserControl(UserControlFactory.BOOKING);
            }
            catch (Exception x)
            {
                MessageBox.Show(x.Message, "Error Exception", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
            }
        }

        void treeNodeBoarding_AfterSelect(object sender, TreeViewEventArgs e)
        {
            try
            {
                loadUserControl(UserControlFactory.BOARDING_CONTROL);
            }
            catch (Exception x)
            {
                MessageBox.Show(x.Message, "Error Exception", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
            }
        }

        void treeNodeTravelAgentSummary_AfterSelect(object sender, TreeViewEventArgs e)
        {
            try
            {
                loadUserControl(UserControlFactory.TRAVEL_AGENT_SUMMARY_REPORT);
            }
            catch (Exception x)
            {
                MessageBox.Show(x.Message, "Error Exception", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
            }
        }

        void treeNodeTravelAgentReport_AfterSelect(object sender, TreeViewEventArgs e)
        {
            try
            {
                loadUserControl(UserControlFactory.TRAVEL_AGENT_REPORT);
            }
            catch (Exception x)
            {
                MessageBox.Show(x.Message, "Error Exception", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
            }
        }

        void treeNodeBookingReport_AfterSelect(object sender, TreeViewEventArgs e)
        {
            try
            {
                loadUserControl(UserControlFactory.BOOKING_REPORT);
            }
            catch (Exception x)
            {
                MessageBox.Show(x.Message, "Error Exception", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
            }
            
        }

        void treeNodeTicketPurchaseReport_AfterSelect(object sender, TreeViewEventArgs e)
        {
            try
            {
                loadUserControl(UserControlFactory.TICKET_PURCHASE_REPORT);
            }
            catch (Exception x)
            {
                MessageBox.Show(x.Message, "Error Exception", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
            }
        }

        void treeNodeConfirmBooking_AfterSelect(object sender, TreeViewEventArgs e)
        {
            try
            {
                loadUserControl(UserControlFactory.CONFIRM_BOOKING_B);
            }
            catch (Exception x)
            {
                MessageBox.Show(x.Message, "Error Exception", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
            }
        }

        void treeNodeCheckIn_AfterSelect(object sender, TreeViewEventArgs e)
        {
            try
            {
                loadUserControl(UserControlFactory.CHECK_IN);
            }
            catch (Exception x)
            {
                MessageBox.Show(x.Message, "Error Exception", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
            }
        }

        void treeNodeTrip_AfterSelect(object sender, TreeViewEventArgs e)
        {
            try
            {
                loadUserControl(UserControlFactory.TRIP);
            }
            catch (Exception x)
            {
                MessageBox.Show(x.Message, "Error Exception", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
            }
        }

        void treeNodeVessel_AfterSelect(object sender, TreeViewEventArgs e)
        {
            try
            {
                loadUserControl(UserControlFactory.VESSEL);
            }
            catch (Exception x)
            {
                MessageBox.Show(x.Message, "Error Exception", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
            }
        }

        void treeNodeSeaPort_AfterSelect(object sender, TreeViewEventArgs e)
        {
            try
            {
                loadUserControl(UserControlFactory.SEAPORT);
            }
            catch (Exception x)
            {
                MessageBox.Show(x.Message, "Error Exception", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
            }
        }

        void treeNodePassport_AfterSelect(object sender, TreeViewEventArgs e)
        {
            try
            {
                loadUserControl(UserControlFactory.PASSPORT);
            }
            catch (Exception x)
            {
                MessageBox.Show(x.Message, "Error Exception", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
            }
        }

        void treeNodeTicketPurchase_AfterSelect(object sender, TreeViewEventArgs e)
        {
            try
            {
                loadUserControl(UserControlFactory.TICKET_PURCHASE);
            }
            catch (Exception x)
            {
                MessageBox.Show(x.Message, "Error Exception", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
            }
        }

        void treeNodeTravelAgent_AfterSelect(object sender, TreeViewEventArgs e)
        {
            try
            {
                loadUserControl(UserControlFactory.TRAVEL_AGENT);
            }
            catch (Exception x)
            {
                MessageBox.Show(x.Message, "Error Exception", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
            }
        }

        void treeNodeConfiguration_AfterSelect(object sender, TreeViewEventArgs e)
        {
            try
            {
                loadUserControl(UserControlFactory.CONFIGURATION);
            }
            catch (Exception x)
            {
                MessageBox.Show(x.Message, "Error Exception", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
            }
        }

        void treeNodeUserRole_AfterSelect(object sender, TreeViewEventArgs e)
        {
            try
            {
                loadUserControl(UserControlFactory.USER_ROLE);
            }
            catch (Exception x)
            {
                MessageBox.Show(x.Message, "Error Exception", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
            }
        }

        void treeNodeUser_AfterSelect(object sender, TreeViewEventArgs e)
        {
            try
            {
                loadUserControl(UserControlFactory.USER);
            }
            catch (Exception x)
            {
                MessageBox.Show(x.Message, "Error Exception", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
            }
        }

        private void loadUserControl(string p)
        {
            splitContainer1.Panel2.Controls.Clear();
            m_userControlFactory.GetUserControl(p).Dock = DockStyle.Fill;
            splitContainer1.Panel2.Controls.Add(m_userControlFactory.GetUserControl(p));
        }

        private void initLoginForm()
        {
            m_loginForm = new UserControlLogin(this);
            m_loginForm.Dock = DockStyle.Fill;
            this.Controls.Add(m_loginForm);
        }
        public void ShowMainFormPanel(bool show)
        {
            panelMainForm.Visible = show;
        }
        public User User
        {
            get { return m_user; }
            set
            { 
                m_user = value;
                if (m_user.PasswordExpired && m_user.ExpiryDate.Subtract(DateTime.Now).Days < 7)
                {
                    MessageBox.Show("Your password will expiry within " + m_user.ExpiryDate.Date.Subtract(DateTime.Today).Days + " days.", "Password Expiration", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);
                }
                m_user.SESSION_ID = this.Session.SessionID;
                textBoxUserName.Text = m_user.UserName + " [ " + m_user.TravelAgent.Name + " ]";
                labelAgent.Text = m_user.TravelAgent.Name;// +" : " + this.Context.HttpContext.Request.ServerVariables["remote_addr"];
                splitContainer1.Panel2.Controls.Clear();
                m_userControlFactory = new UserControlFactory(m_user, this);
                initTreeViewButtonPermission();
                if (treeNodeCheckIn.Visible)
                    treeViewMenu.SelectedNode = treeNodeCheckIn;
            }
        }

        public void RefreshUserAndPermission(User user)
        {
            User = user;
        }
        
        private void initTreeViewButtonPermission()
        {
            treeViewMenu.SelectedNode = null;
            treeViewMenu.CollapseAll();
            //Admin Root
            treeNodeAdmin.Visible = m_user.RoleDetails(UserControlFactory.TRAVEL_AGENT).ViewFlag
                || m_user.RoleDetails(UserControlFactory.USER).ViewFlag
                || m_user.RoleDetails(UserControlFactory.USER_ROLE).ViewFlag
                || m_user.RoleDetails(UserControlFactory.PASSPORT).ViewFlag
                || m_user.RoleDetails(UserControlFactory.CONFIGURATION).ViewFlag
                || m_user.RoleDetails(UserControlFactory.SEAPORT).ViewFlag
                || m_user.RoleDetails(UserControlFactory.VESSEL).ViewFlag
                || m_user.RoleDetails(UserControlFactory.TRIP).ViewFlag
                || m_user.RoleDetails(UserControlFactory.MEMBER_TYPE).ViewFlag;

            treeNodeTravelAgent.Visible = m_user.RoleDetails(UserControlFactory.TRAVEL_AGENT).ViewFlag;
            treeNodeUser.Visible = m_user.RoleDetails(UserControlFactory.USER).ViewFlag;
            treeNodeUserRole.Visible = m_user.RoleDetails(UserControlFactory.USER_ROLE).ViewFlag;
            treeNodePassport.Visible = m_user.RoleDetails(UserControlFactory.PASSPORT).ViewFlag;
            treeNodeConfiguration.Visible = m_user.RoleDetails(UserControlFactory.CONFIGURATION).ViewFlag;
            treeNodeSeaPort.Visible = m_user.RoleDetails(UserControlFactory.SEAPORT).ViewFlag;
            treeNodeVessel.Visible = m_user.RoleDetails(UserControlFactory.VESSEL).ViewFlag;
            treeNodeTrip.Visible = m_user.RoleDetails(UserControlFactory.TRIP).ViewFlag;
            treeNodeMemberType.Visible = m_user.RoleDetails(UserControlFactory.MEMBER_TYPE).ViewFlag;
            //Boooking
            treeNodeBooking.Visible = m_user.RoleDetails(UserControlFactory.BOOKING).ViewFlag;
            //check In Control
            treeNodeConfirmBooking_b.Visible = m_user.RoleDetails(UserControlFactory.CONFIRM_BOOKING).ViewFlag;
            //CheckIn root
            treeNodeCheckIn.Visible = m_user.RoleDetails(UserControlFactory.CHECK_IN).ViewFlag;
            //Confirm Booking root
            treeNodeConfirmBooking.Visible = false; //m_user.RoleDetails(UserControlFactory.CONFIRM_BOOKING).ViewFlag;
            //Ticket Purchase root
            treeNodeTicketPurchase.Visible = m_user.RoleDetails(UserControlFactory.TICKET_PURCHASE).ViewFlag;
            //TRANSFER PSG
            treeNodeTransferPassenger.Visible = m_user.RoleDetails(UserControlFactory.TRANSFER_PASSENGER).ViewFlag;
            //
            treeNodeTicketDeduction.Visible = m_user.RoleDetails(UserControlFactory.TICKET_DEDUCTION).ViewFlag;
            //Report Root
            treeNodeReport.Visible = m_user.RoleDetails(UserControlFactory.TICKET_PURCHASE_REPORT).ViewFlag
                || m_user.RoleDetails(UserControlFactory.BOOKING_REPORT).ViewFlag
                || m_user.RoleDetails(UserControlFactory.TRAVEL_AGENT_REPORT).ViewFlag
                || m_user.RoleDetails(UserControlFactory.TRAVEL_AGENT_SUMMARY_REPORT).ViewFlag;
            treeNodeTicketPurchaseReport.Visible = m_user.RoleDetails(UserControlFactory.TICKET_PURCHASE_REPORT).ViewFlag;
            treeNodeTicketDeductionReport.Visible = m_user.RoleDetails(UserControlFactory.TICKET_DEDUCTION_REPORT).ViewFlag;
            treeNodeBookingReport.Visible = m_user.RoleDetails(UserControlFactory.BOOKING_REPORT).ViewFlag;
            treeNodeTravelAgentReport.Visible = m_user.RoleDetails(UserControlFactory.TRAVEL_AGENT_REPORT).ViewFlag;
            treeNodeTravelAgentSummary.Visible = m_user.RoleDetails(UserControlFactory.TRAVEL_AGENT_SUMMARY_REPORT).ViewFlag;
            treeNodeBoarding.Visible = m_user.RoleDetails(UserControlFactory.BOARDING_CONTROL).ViewFlag;
            treeNodeTicketReport.Visible = m_user.RoleDetails(UserControlFactory.TICKET_REPORT).ViewFlag;
            
            //other role..................
        }

        private void buttonSignOut_Click(object sender, EventArgs e)
        {
            ShowMainFormPanel(false);
            m_loginForm.InsertlogoutSession();
            m_loginForm.SetMessage("You have successfully signed out of Wave Master Holiday Club App.");
            m_loginForm.Show();
            m_userControlFactory.ClearUserControls();
           
            m_user = null;
        }

        private void buttonUserProfile_Click(object sender, EventArgs e)
        {
            UserProfileForm usp = new UserProfileForm(m_user);
            usp.ShowDialog();
        }

        //private ListViewItem CreateListViewLargeItem()
        //{
        //    ListViewItem lvi = new ListViewItem();
        //    lvi.BackColor = System.Drawing.SystemColors.Window;
        //    lvi.Font = new System.Drawing.Font("Tahoma", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
        //    lvi.ForeColor = System.Drawing.SystemColors.WindowText;
        //    System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(MainForm));
        //    lvi.LargeImage = new Gizmox.WebGUI.Common.Resources.ImageResourceHandle(resources.GetString("listViewItemSoftmar.LargeImage"));
        //    lvi.Text = "Softmar";
        //    return lvi;
        //}
    }
}